package com.oceansoft.core.jxkh.service.impl;

import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.jxkh.dao.ITaskDao;
import com.oceansoft.core.jxkh.domain.Khfk;
import com.oceansoft.core.jxkh.domain.Task;
import com.oceansoft.core.jxkh.service.ITaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by begcoffee on 2017/12/11.
 */
@Service("taskservice")
public class TaskServiceImpl implements ITaskService {
    @Resource
    private ITaskDao taskDao;

    @Override
    public Pagination querybycondition(HashMap<String, String> map, SysUser user) {
        return taskDao.querybycondition(map, user);
    }

    @Override
    public void addTask(Task task) throws Exception {
        taskDao.addTask(task);
    }
    @Override
    public void saveTask(Task task) throws Exception {
        taskDao.saveTask(task);
    }

    @Override
    public void deleteTask(Long dbid) throws Exception {
        taskDao.deleteTask(dbid);
    }

    @Override
    public int stopTask(Long dbid,String type) throws Exception {
        return taskDao.stopTask(dbid,type);
    }

    @Override
    public Task getTaskById(Long dbid) throws Exception {
        return taskDao.getTaskById(dbid);
    }

    @Override
    public void saveKhfk(Khfk khfk) throws Exception {
        taskDao.saveKhfk(khfk);
    }

    @Override
    public Khfk getKhfkById(Long dbid) throws Exception {
        return taskDao.getKhfkById(dbid);
    }

    /***
     * 任务进度服务
     * 每日凌晨2点进行任务推进
     */
    public void doTaskProcess(){
        //推进开始任务
        startTaskProcess();
        System.out.println(new Date()+"------------推进开始任务完成");
        //推进结束任务
        stopTaskProcess();
        System.out.println(new Date()+"------------推进结束任务完成");
    }

    /***
     * //查询需要开始的待考核任务
     //开始本任务，从待考核改为考核中
     //初始化自评、打分数据
     //推送提醒
     */
    private void startTaskProcess(){
        try {
            List<Task> startingTaskList =  taskDao.getStartingTask();
            for (Task task: startingTaskList) {
                task.setStatus(Constant.TASKDOING);
                taskDao.updateTaskStatus(task);
                taskDao.initTaskMark(task);
                //TODO 推送提醒
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 查询需要结束的任务
     * 结束本任务
     * 找到下一个任务
     * 传递考核关系到下一个任务
     * 将下一个任务从待设置改为待考核
     */
    private void stopTaskProcess(){
        try {
            List<Task> endingTaskList =  taskDao.getEndingTask();
            for (Task task: endingTaskList) {
                task.setStatus(Constant.TASKDONE);
                taskDao.updateTaskStatus(task);

                Task nextTask = taskDao.getNextTask(task);
                if(nextTask != null) {
                    taskDao.doCopyTaskRelation(task, nextTask);
                    nextTask.setStatus(Constant.TASKREADY);
                    taskDao.updateTaskStatus(nextTask);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
